<?php
require_once(ROOTDIR.'classes/basedb.php');

class AlbumDB extends BaseDB{
	public function getAlbumGoods($albumid,$stime){
		$sql = sprintf("select a.album_id,a.share_id,a.cid,b.create_time,b.comment_count,b.collect_count,c.goods_id,c.name,c.img,c.url,c.price,c.taoke_url,c.goods_key,c.img_height,c.img_width,c.keyimg_url
							from ".TB_HEAD."_album_share a,".TB_HEAD."_share b,".TB_HEAD."_share_goods c where a.share_id=b.share_id and a.share_id=c.share_id and a.album_id = %s and b.create_time >= %d and b.create_time<%d and c.goods_key like 'taobao%%'",$albumid,$stime,$stime+24*60*60);
		echo $sql."\r\n";
	    $query = $this->_db_reader->query($sql);
	    $result = $this->_getAllResultFromQuery($query);	
	    return $result;
	}
	
	public function getAlbumGoodsByAlbumList($albumids,$stime){
		$sql = sprintf("select a.album_id,a.share_id,a.cid,(b.create_time+8*60*60) as create_time,b.comment_count,b.collect_count,c.goods_id,c.name,c.img,c.url,c.price,c.taoke_url,c.goods_key,c.img_height,c.img_width,c.keyimg_url
							from ".TB_HEAD."_album_share a,".TB_HEAD."_share b,".TB_HEAD."_share_goods c where a.share_id=b.share_id and a.share_id=c.share_id and a.album_id in (%s) and b.create_time+8*60*60 >= %d and b.create_time+8*60*60<%d and c.goods_key like 'taobao%%'",$albumids,$stime,$stime+24*60*60);
		echo $sql."\r\n";
	    $query = $this->_db_reader->query($sql);
	    $result = $this->_getAllResultFromQuery($query);	
	    return $result;
	}
	
	public function getAlbumGoodsByAlbumListByFlag($albumids,$stime){
		$sql = sprintf("select a.album_id,a.share_id,a.cid,(b.create_time+8*60*60) as create_time,b.comment_count,b.collect_count,c.goods_id,c.name,c.img,c.url,c.price,c.taoke_url,c.goods_key,c.img_height,c.img_width,c.keyimg_url,c.syn_count 
							from ".TB_HEAD."_album_share a,".TB_HEAD."_share b,".TB_HEAD."_share_goods c where a.share_id=b.share_id and a.share_id=c.share_id and a.album_id in (%s) and c.syn_flag=0 and c.syn_count<3 and b.create_time+8*60*60<%d and c.goods_key like 'taobao%%'",$albumids,$stime+24*60*60);
		echo $sql."\r\n";
	    $query = $this->_db_reader->query($sql);
	    $result = $this->_getAllResultFromQuery($query);	
	    return $result;
	}
	
	public function getAlbumGoodsByKeyList(){
		$keylist = "'taobao_14491105125','taobao_14997751961','taobao_14031049868','taobao_14517449390','taobao_14444890775','taobao_16652948512','taobao_9805595697','taobao_14463270294','taobao_16856836112','taobao_14877643584','taobao_16484200306','taobao_16586252656','taobao_15037603192','taobao_15703324545','taobao_14589295887','taobao_14410893408','taobao_16618716716','taobao_16144120534','taobao_14178985480','taobao_16478756961','taobao_16463852764','taobao_16639856829','taobao_14014081306','taobao_14272349375','taobao_9731836679','taobao_9996912813','taobao_14238878216','taobao_13623757699','taobao_15874288812','taobao_14053914660','taobao_16688820955','taobao_15918796023','taobao_10886908995','taobao_13623413444','taobao_12648692693','taobao_16100376570','taobao_14821727745','taobao_14419562399','taobao_9632321734','taobao_6127059143','taobao_10441633301','taobao_16578252506','taobao_14233935587','taobao_9706732086','taobao_9573598156','taobao_16585232435','taobao_16180536962','taobao_16561496091','taobao_14183881295','taobao_8233171984','taobao_14139642973','taobao_14269917740','taobao_12703592437','taobao_14103787324','taobao_16182780853','taobao_14971327165','taobao_16169648827','taobao_14893059837','taobao_13722106155','taobao_15997608335','taobao_14352819968','taobao_14645607991','taobao_14807179746','taobao_10755910424','taobao_15991700845','taobao_13728073356','taobao_14831415579','taobao_15100087287','taobao_16599664824','taobao_16394608032','taobao_14264222119','taobao_16142312890','taobao_16380628518','taobao_14543358331','taobao_13770096947','taobao_14018374610','taobao_10442037493','taobao_14964247448','taobao_14134607766','taobao_14778803907','taobao_13783801509','taobao_14239041746','taobao_14039786862','taobao_14908219962','taobao_13736122376','taobao_16860616873','taobao_14449910747','taobao_14415218376','taobao_14951071994','taobao_14372457938','taobao_7028723537','taobao_16403204352','taobao_16793392276','taobao_14416017228','taobao_16250948673','taobao_14688659141','taobao_15006067542','taobao_16735140024','taobao_14467538573','taobao_14776215860','taobao_12685806343','taobao_14438257881','taobao_14689375186','taobao_15167155456','taobao_14393781768','taobao_14388254911','taobao_14263729732','taobao_14992375951','taobao_10261390720','taobao_16788408410','taobao_15263499540','taobao_14451538392','taobao_16811512402','taobao_12464523277','taobao_6379808043','taobao_15122515348','taobao_16921744016','taobao_14511229531','taobao_13294953963','taobao_14542093261','taobao_16294060843','taobao_7262477382','taobao_14228134268','taobao_16897448866','taobao_14729163213','taobao_16131192190','taobao_16607832466','taobao_14087185991','taobao_14968823865','taobao_14303291456','taobao_14195179196'";
		$sql = sprintf("select a.album_id,a.share_id,a.cid,(b.create_time+8*60*60) as create_time,b.comment_count,b.collect_count,c.goods_id,c.name,c.img,c.url,c.price,c.taoke_url,c.goods_key,c.img_height,c.img_width,c.keyimg_url,c.syn_count 
							from ".TB_HEAD."_album_share a,".TB_HEAD."_share b,".TB_HEAD."_share_goods c where a.share_id=b.share_id and a.share_id=c.share_id and c.goods_key in (%s)",$keylist);
		echo $sql."\r\n";
	    $query = $this->_db_reader->query($sql);
	    $result = $this->_getAllResultFromQuery($query);	
	    return $result;		
	}
	
	public function getGoodsItemByGoodsKey($key){
		$sql = sprintf("select * from ".TB_HEAD."_share_goods where goods_key='%s'",$key);
		$result = $this->_db_reader->get_one($sql);
		return $result;
	}	
	
	public function getGoodsByGoodsKey($key){
		$sql = sprintf("select count(goods_id) as c from ".TB_HEAD."_share_goods where goods_key='%s' and syn_flag in (0,2) and syn_count<=3",$key);
		$result = $this->_db_reader->get_one($sql);
		if($result && $result['c']>=1) return TRUE;
		else return FALSE;
	}
	
	public function updateGoodsSyn($key){
		$sql = sprintf("update ".TB_HEAD."_share_goods set syn_flag=1,update_time=%d where goods_key='%s'",time(),$key);
		echo $sql."\r\n";
		$this->_db_writer->update($sql);
		return $this->_db_writer->affected_rows();
	}
	
	public function updateGoodsSynCount($key,$count,$max_count=3){
		if($count<$max_count){
			$sql = sprintf("update ".TB_HEAD."_share_goods set syn_flag=0,syn_count=%d,update_time=%d where goods_key='%s'",$count,time(),$key);
		}else{
			$sql = sprintf("update ".TB_HEAD."_share_goods set syn_flag=2,syn_count=3,update_time=%d where goods_key='%s'",time(),$key);
		}
		echo $sql."\r\n";
		$this->_db_writer->update($sql);
		return $this->_db_writer->affected_rows();
	}
	
	public function getEtaoIdList(){
		$sql = "select distinct etao_id from ".TB_HEAD."_album_etao where status=1";
		$query = $this->_db_reader->query($sql);
		$result = $this->_getAllResultFromQuery($query);	
		return $result;
	}
	
	public function getAlbumEtaoRelation($etaoid){
		$sql = "select distinct album_id from ".TB_HEAD."_album_etao where etao_id=".$etaoid." and status=1";
		$query = $this->_db_reader->query($sql);
		$result = $this->_getAllResultFromQuery($query);	
		return $result;
	}
	
	public function insertEtaoLog($etao_id,$iday,$down_url,$size,$line){
		$sql = sprintf("insert into ".TB_HEAD."_album_etao_log(etao_id,iday,down_url,size,line,create_time) values(%d,'%s','%s',%d,%d,%d)",$etao_id,$iday,$down_url,$size,$line,time());
		echo $sql."\r\n";
		$this->_db_writer->update($sql);
		return $this->_db_writer->affected_rows();
	}
	
	public function getAlbumCategoryById($cid){
		$sql = sprintf("select * from ".TB_HEAD."_album_category where id=%d",$cid);
		return $this->_db_reader->get_one($sql);
	}
	
	public function getShareTagList($shareid){
		$sql = sprintf("select tag_name from ".TB_HEAD."_share_tags where share_id=%d",$shareid);
	    $query = $this->_db_reader->query($sql);
	    $result = $this->_getAllResultFromQuery($query);	
	    return $result;
	}
}
?>